package gw.com.android.app.dispatch;

import android.app.Activity;
import android.text.TextUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 * APP 内部的任务分发处理
 * 根据 url 处理 APP 内不同的业务。
 * <p>
 * hxgts2://home/trade_strategy/
 * hxgts2://quote/tab/
 * hxgts2://trade/create_order?ui_code=123&is_buy=sdfs
 * hxgts2://discuss/
 * hxgts2://mine/
 * hxgts2://other/
 *
 * @author jett
 * @since 2018-05-16.
 */
public class AppDispatch {

    public static final String SCHEME = "hxgts2";

    private static final String HOME = "home";
    private static final String QUOTE = "quote";
    private static final String TRADE = "trade";
    private static final String DISCUSS = "discuss";
    private static final String MINE = "mine";
    private static final String OTHER = "other";

    public static boolean dispatch(Activity activity, String param) {
        if (TextUtils.isEmpty(param)) {
            return false;
        }
        param = URLDecoder.decode(param);
        try {
            URI uri = new URI(param);
            if (!SCHEME.equals(uri.getScheme())) {
                return false;
            }
            Dispatch dispatch = createDispatch(uri.getHost());
            if (dispatch == null) {
                return false;
            }
            String path = uri.getPath();
            String query = uri.getQuery();
            if (path != null && path.length() > 0) {
                //  去掉 path 首尾 的 '/'  符号
                if (path.charAt(0) == '/') {
                    path = path.substring(1);
                }
                if (path.length() > 0 && path.charAt(path.length() - 1) == '/') {
                    path = path.substring(0, path.length() - 1);
                }
            }
            Map<String, String> params = parseQuery(query);
            return dispatch.handle(activity, path, params);
        } catch (URISyntaxException e) {
            return false;
        }
    }

    private static Map<String, String> parseQuery(String query) {
        if (!TextUtils.isEmpty(query)) {
            String[] split = query.split("&");
            if (split.length > 0) {
                Map<String, String> map = new HashMap<>();
                for (String str : split) {
                    if (TextUtils.isEmpty(str)) {
                        continue;
                    }
                    String[] params = str.split("=");
                    if (params.length >= 2) {
                        map.put(params[0], params[1]);
                    }
                }
                return map;
            }
        }
        return null;
    }

    private static Dispatch createDispatch(String host) {
        switch (host) {
            case HOME:
                return new HomeDispatch();
            case QUOTE:
                return new QuoteDispatch();
            case TRADE:
                return new TradeDispatch();
            case DISCUSS:
                return new DiscussDispatch();
            case MINE:
                return new MineDispatch();
            case OTHER:
                return new OtherDispatch();
            default:
                return null;
        }
    }

}
